'use strict';

const Controller = require('egg').Controller;
const _trim = require('lodash/trim');

class UserController extends Controller {
    async index() {
        const {
            ctx
        } = this;

        const user = await ctx.service.user.getUserById(ctx.query.id);
        if (!user) {
            ctx.body = ctx.helper.apiBackMessage('用户不存在');
            return;
        }

        ctx.body = ctx.helper.apiBackMessage('', '0', user);
    }

    async login() {
        const {
            ctx
        } = this;
        const apiBackMessage = ctx.helper.apiBackMessage;

        let {
            account,
            password
        } = ctx.query;

        account = _trim(account);
        password = _trim(password);

        if (account.length === 0 || password.length === 0) {
            ctx.body = apiBackMessage('账号密码不能为空');
            return;
        }

        const user = await ctx.service.user.getUserLogin(account, password);
        if (user.length) {
            ctx.body = apiBackMessage('登陆失败');
        } else {
            ctx.body = apiBackMessage('', '0', user);
        }
    }
}

module.exports = UserController;